package com.miaosmart.dto;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.ai.chat.messages.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MessageDTO {
    //信息类型：是AI的，还是用户的，或者是系统的
    MessageType messageType;
    //对话的文本信息
    String text;
    //其他信息
    Map<String, Object> metadata = new HashMap<>();

    //将SpringAI的Message转为我们的Msg
    public MessageDTO(Message message) {
        this.messageType = message.getMessageType();
        this.text = message.getText();
        this.metadata = message.getMetadata() != null ? message.getMetadata() : new HashMap<>();
    }

    //实现将我们的Msg转为SpringAI的Message
    public Message toMessage() {
        Map<String, Object> safeMetadata = metadata != null ? metadata : new HashMap<>();
        return switch (messageType) {
            case SYSTEM -> new SystemMessage(text);
            case USER -> new UserMessage(text, List.of(), safeMetadata);
            case ASSISTANT -> new AssistantMessage(text, safeMetadata, List.of(), List.of());
            default -> throw new IllegalArgumentException("Unsupported message type: " + messageType);
        };
    }

}
